package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 检查项服务接口实现类
 */
@Service(interfaceClass = CheckItemService.class)  //interfaceClass:指定服务提供方实现的 interface 的类
@Transactional  //事务
public class CheckItemServiceImpl implements CheckItemService {
    @Resource
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        int page = queryPageBean.getCurrentPage();
        int size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询，是基于mybatis框架提供的分页插件完成
        PageHelper.startPage(page,size);
        Page<CheckItem> all = checkItemDao.findByCondition(queryString);
        return new PageResult(all.getTotal(),all.getResult());
    }

    @Override
    public void delete(Integer id) throws RuntimeException{
        //查询当前检查项是否和检查组有关联
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            //当前检查项有和检查组引用，无法删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        //删除检查项
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }


    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = checkItemDao.findAll();
        return checkItemList;
    }
}
